package com.aier.user.action;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.common.CommonSession;
import com.aier.common.common.Constant;
import com.aier.common.common.ICommonDAO;
import com.aier.common.common.ImageUtil;
import com.aier.user.entity.Follow;
import com.aier.user.entity.Fresh;
import com.aier.user.entity.User;
import com.aier.user.entity.UserPhotoImg;
import com.aier.user.entity.UserPhotoType;
import com.aier.user.service.IFindPeopleService;
import com.aier.user.service.IHomeForumsService;
import com.aier.user.service.IMyFollowService;
import com.aier.user.service.IUserService;

@Scope("prototype")
@Component("userCenterCommonAction")
public class UserCenterCommonAction extends UserBaseAction {

    /**
     * 个人中心COMMON
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "userService")
    private IUserService userService;
    @Resource(name = "homeForumsService")
    private IHomeForumsService homeForumsService;
    @Resource(name = "findPeopleService")
    private IFindPeopleService findPeopleService;
    @Resource(name="myFollowService")
    private IMyFollowService myFollowService;

    private int s; // 成功:1 ,失败:0
    private String error_info; // 错误提示信息
    
    private List<UserPhotoType> uptList;// (用户相册分类)列表 
    private List<UserPhotoImg> upiList;// (用户相册图片)列表
    private UserPhotoType upt; // 用户相册
    private List<User> babyList; // 宝宝列表
    private Long uid;//登录的用户ID
    private Long in_uid; // 个人主页主人的uid
    private User user; // 用户
    private Long uptid; // 相册id
    private Long upiid; // 相册图片id
    private String pname; // 相册名称
    private Short state; // 权限
    private String img; // 相册封面图片文件名or相册图片文件名
    private String converimg; // 个人封面主图
    private File file;
    private File file2;
    private String photoName; // 照片原名

    private Integer in_type; // 进入类型
                                // 1:我的内容,2:我的资料,3:我的相册(相册类别列表)4:我的收藏,5:我的相册(相册图片列表)
    private Integer isOther; // 1:是别人 0:是自己

    private int page; // 页数
    private int page_size; // 每页的记录数
    private int page_count; // 总页数
    private int count; // 总记录数
    private List<Fresh> freshList; // 新鲜事列表

    private String selCoId;// 默认图名称
    
    private String nickname; // 昵称
    private boolean isGZ;//是否关注
   

    @Override
    public String execute() {
        Long uid = null;
        if (nickname !=null && !"".equals(nickname)) {
            User user = userService.searchUserByNickname(nickname);
            in_uid = user.getUid();
            uid = in_uid;
            if (in_uid.equals((Long) session.get(CommonSession.SESSION_ACCOUNT_ID))) {
                isOther = 0;
            } else {
                isOther = 1;
                // 新增访客记录
                findPeopleService.insertVisitor((Long) session.get(CommonSession.SESSION_ACCOUNT_ID),in_uid);
            }
            
        } else {
            if (in_uid == null) {
                uid = (Long) session.get(CommonSession.SESSION_ACCOUNT_ID);
                in_uid = uid;
                isOther = 0;
            } else {
                uid = in_uid;
                if (in_uid.equals((Long) session.get(CommonSession.SESSION_ACCOUNT_ID))) {
                    isOther = 0;
                } else {
                    isOther = 1;
                    // 新增访客记录
                    findPeopleService.insertVisitor((Long) session.get(CommonSession.SESSION_ACCOUNT_ID),in_uid);
                }
            }
        }

        user = findPeopleService.searchSelfInfo(uid);

        if (in_type != null && in_type == 2) {
            babyList = userService.searchUserBabyList(uid);
        }
        if (in_type != null && in_type == Constant.num_3) {
            uptList = userService.searchUserPhotoTypeList(uid);
        }
        
        if (in_type != null && in_type == Constant.num_5) {
            upt = userService.searchUserPhotoType(uptid);
            upiList = userService.searchUserPhotoImgList(uptid);
        }

        return SUCCESS;
    }
    
    /**
     * APP端获取方法
     * @return
     */
    public String userInfoapp(){
      
        if(in_uid==null || uid ==null ){
        	s=0;
        	error_info="用户不存在";
        	return SUCCESS;
        }
        user = findPeopleService.searchSelfInfo(in_uid);
        	 Follow f =myFollowService.searchFriend(uid,in_uid);
        	 if(f !=null){
        		 if(f.getRemark()!=null)
        		 user.setRemark(f.getRemark());
        	 }
        	 
        babyList = userService.searchBabyByList(in_uid);       
        
        //查询好友是否已经被关注
        s = myFollowService.searchFollowByUid(uid, in_uid);
        
        if(s>0)
        	isGZ=true;
        else
        	isGZ=false;
        
    	 return SUCCESS;
    }

    /**
     * 个人封面设置
     * 
     * @param updateUserConverimg
     *            ()上传个人封面图片
     * @param deleteUserConverimg
     *            ()删除个人封面图片
     * @param updateUImgName
     *            ()将个人封面图片名存入数据库
     * @param serachConverimg
     *            ()读取个人封面图片
     */
    // 上传个人封面图片
    public String updateUserConverimg() {
        converimg = ImageUtil.uploadImageAdjustable(file, upload_converimg_url,
                photoName, Constant.num_269, Constant.num_82, Constant.num_980,
                Constant.num_300, true);
        return SUCCESS;
    }

    // 删除个人封面图片

    public String deleteUserConverimg() {
        File fo = new File(upload_converimg_url + photoName);
        if (fo.exists()) {
            fo.delete();
        }
        s = 1;
        return SUCCESS;
    }

    // 将个人封面图片名存入数据库
    public String updateUImgName() {
        if (userService.updateUserCoverimg(converimg, (Long) request
                .getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID)) == 0) {
            s = 0;
            error_info = "修改个人封面失败";
        }
        s = 1;
        return SUCCESS;
    }

    // 设置给定图片为封面
    public String updateGivImgName() {

        if (userService.updateUserCoverimg(selCoId + ".jpg", (Long) request
                .getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID)) == 0) {
            s = 0;
            error_info = "修改个人封面失败";
        }
        s = 1;
        return SUCCESS;
    }

    // 读取个人封面图片
    public String serachConverimg() {
        User u = userService.getUserByUid(in_uid);
        if (null != u) {
            converimg = u.getCoverimg();
        }
        return SUCCESS;
    }

    /**
     * 个人相册
     * 
     * @param searchUserPhotoImg_type
     *            ()查询某相册下的图片列表
     */
    // 相册类别查询
    public String searchUserImgtype() {
        uptList = userService.searchUserPhotoTypeList(in_uid);
        return SUCCESS;
    }

    // 查询某相册下的图片列表
    public String searchUserPhotoImg_type() {
        upiList = userService.searchUserPhotoImgList(uptid);
        return SUCCESS;
    }

    // 创建相册
    public String addUserPhotoType() {
        UserPhotoType upt = new UserPhotoType();
        upt.setUid(in_uid);
        upt.setPname(pname);
        upt.setState((short) 0);
        userService.insertUserPhotoType(upt);
        s = 1;
        return SUCCESS;
    }

    // 上传相册图片到服务器
    public String uploadUserPhotoImg() {
        img = ImageUtil.uploadImageAdjustable(file2, upload_gallery_url,
                photoName, Constant.num_200, Constant.num_200,
                Constant.num_500, Constant.num_500, false);
        return SUCCESS;
    }

    // 删除上传的照片
    public String deleteUploadImg() {
        File fo1 = new File(upload_gallery_url + "big/" + img);
        File fo2 = new File(upload_gallery_url + "origin/" + img);
        File fo3 = new File(upload_gallery_url + "small/" + img);
        if (fo1.exists()) {
            fo1.delete();
        }
        if (fo2.exists()) {
            fo2.delete();
        }
        if (fo3.exists()) {
            fo3.delete();
        }
        s = 1;
        return SUCCESS;
    }

    // 上传图片到相册
    public String addUserPhotoImg() {
        UserPhotoImg upi = new UserPhotoImg();
        upi.setUptid(uptid);
        upi.setImg(img);
        Long upiid = userService.insertUserPhotoImg(upi);
        userService.updateUserPhotoImg_face(upiid);
        s = 1;
        return SUCCESS;
    }

    // 删除相册图片
    public String deleteUserPhotoImg() {
        if (null == upiid) {
            s = 0;
            return SUCCESS;
        }
        UserPhotoImg upi = userService.searchUserPhotoImg(upiid);
        s = userService.deleteUserPhotoImg(upiid);
        s = userService.updateUserPhotoTypeCover("", upi.getUptid());
        return SUCCESS;
    }
    

    
    /**
     * 获取本人的新鲜事
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchFreshSelf() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = homeForumsService.searchFreshSelf(page,
                page_size, in_uid);

        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            
            freshList = (List<Fresh>) map.get(ICommonDAO.OBJ_LIST);

            if (null != freshList && freshList.size() > 0) {
                for (Fresh fresh : freshList) {
                    if (fresh.getType() == 1) {
                        fresh.setFresh(homeForumsService.searchFresh(
                                fresh.getRelativeid(), 1));
                    }
                    // 是否已赞过（1赞过0没赞）
                    if (null != homeForumsService.searchPraiseByCon(
                            (Long) request.getSession().getAttribute(
                                    CommonSession.SESSION_ACCOUNT_ID),
                            fresh.getFid())) {
                        fresh.setPflag((short) 1);
                    } else {
                        fresh.setPflag((short) 0);
                    }
                    // 是否已收藏
                    if (null != homeForumsService.searchFavoriteByCon(
                            (Long) request.getSession().getAttribute(
                                    CommonSession.SESSION_ACCOUNT_ID),
                            fresh.getFid())) {
                        fresh.setFflag((short) 1);
                    } else {
                        fresh.setFflag((short) 0);
                    }
                }
            }
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 用户中心投诉
     * @return
     */
    public String  Complaints() {
    	 s = 1;
    	//uid 登录的用户 in_uid被投诉的用户
         return SUCCESS;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public void setPhotoName(String photoName) {
        this.photoName = photoName;
    }

    public String getConverimg() {
        return converimg;
    }

    public void setConverimg(String converimg) {
        this.converimg = converimg;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public List<UserPhotoType> getUptList() {
        return uptList;
    }

    public List<User> getBabyList() {
        return babyList;
    }

    public UserPhotoType getUpt() {
        return upt;
    }

    public List<UserPhotoImg> getUpiList() {
        return upiList;
    }

    public Long getIn_uid() {
        return in_uid;
    }

    public void setIn_uid(Long in_uid) {
        this.in_uid = in_uid;
    }
    

    public Long getUid() {
		return uid;
	}

	public void setUid(Long uid) {
		this.uid = uid;
	}

	public User getUser() {
        return user;
    }

    public Integer getIn_type() {
        return in_type;
    }

    public Integer getIsOther() {
        return isOther;
    }

    public void setUptid(Long uptid) {
        this.uptid = uptid;
    }

    public void setUpiid(Long upiid) {
        this.upiid = upiid;
    }

    public void setState(Short state) {
        this.state = state;
    }

    public void setFindPeopleService(IFindPeopleService findPeopleService) {
        this.findPeopleService = findPeopleService;
    }

    public void setHomeForumsService(IHomeForumsService homeForumsService) {
        this.homeForumsService = homeForumsService;
    }

    public int getPage_count() {
        return page_count;
    }

    public int getCount() {
        return count;
    }

    public List<Fresh> getFreshList() {
        return freshList;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public void setIn_type(Integer in_type) {
        this.in_type = in_type;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public String getImg() {
        return img;
    }

    public void setFile2(File file2) {
        this.file2 = file2;
    }

    public Long getUptid() {
        return uptid;
    }

    public String getSelCoId() {
        return selCoId;
    }

    public void setSelCoId(String selCoId) {
        this.selCoId = selCoId;
    }

    public int getPage() {
        return page;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

	public boolean isGZ() {
		return isGZ;
	}
}
